//#include <AbstractItemModel>
#include <memory>
#include "TreeModel.h"
#include <QDebug>

TreeModel::TreeModel(QObject *parent) :
    QAbstractItemModel(parent), m_rootItem(nullptr)
{
//    m_Cfg.Init();
//    m_roleNames[Role1] = "nodeText";
}

TreeModel::~TreeModel()
{
    if (m_rootItem)
        delete m_rootItem;
}

QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    return QAbstractItemModel::headerData(section, orientation, role);
}

int TreeModel::columnCount(const QModelIndex &parent) const
{
//    return m_nCoumnCount;
    int nCount;
    //返回实际的列数 (实际是他返回了0，因为根节点用的是无参的构造)，TreeView控件会认为表是空表，不获取数据
    if (parent.isValid())
    {
        nCount = static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    }
    else
    {
        nCount = m_rootItem->columnCount();
    }

    if (nCount <= 0)
        return m_nCoumnCount;
    else
    {
//        qDebug()<<"columnCount()="<<nCount;
        return nCount;
    }
}

QHash<int, QByteArray> TreeModel::roleNames() const
{
    qDebug()<<"m_roleNames.size()="<<m_roleNames.size();
    return m_roleNames;
}

bool TreeModel::isLeafNode(const QModelIndex &index)
{
    TreeItem* pItem = static_cast<TreeItem*>(index.internalPointer());
    return pItem && 0 == pItem->childCount();
}

//int TreeModel::getRowType(const QModelIndex &index)
//{
//    TreeItem* pItem = static_cast<TreeItem*>(index.internalPointer());

//    return (int)pItem->GetType();
//}

QVariant TreeModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    TreeItem* pItem = static_cast<TreeItem*>(index.internalPointer());
    int nDataIndex = role - Role1;
    if (nDataIndex < pItem->columnCount())
        return pItem->data(nDataIndex);
//    QString qsRoleIndexs = m_roleNames[role].data();
//    QStringList qlIndexs = qsRoleIndexs.split(g_GridHeadSep);
////    int nRowType = getRowType(pItem->GetType());
//    if (pItem->GetType() < qlIndexs.size())
//    {
//        QString nColIndex = qlIndexs.at(pItem->GetType());
//        int nDataIndex = nColIndex.toInt();
//        if (nDataIndex < pItem->columnCount())
//            return pItem->data(nDataIndex);
//    }

    return QVariant();
}

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent))
    {
        return QModelIndex();
    }

    TreeItem *parentItem;
    if (!parent.isValid())
    {
        parentItem = m_rootItem;
    }
    else
    {
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
    }

    TreeItem *childItem = parentItem->child(row);
//    qDebug()<<"index:"<<row<<","<<column<<" "<<childItem;
    if (childItem)
    {
        return createIndex(row, column, childItem);
    }
    else
    {
        return QModelIndex();
    }
}

QModelIndex TreeModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return QModelIndex();
    }

    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    TreeItem *parentItem = childItem->parent();

    if (parentItem == m_rootItem)
    {
        return QModelIndex();
    }

    return createIndex(parentItem->row(), 0, parentItem);
}

int TreeModel::rowCount(const QModelIndex &parent) const
{
    TreeItem *parentItem;
    if (parent.column() > 0 || nullptr == m_rootItem)
    {
        return 0;
    }


    if (!parent.isValid())
    {
        parentItem = m_rootItem;
    }
    else
    {
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
    }

    return parentItem->childCount();
}

void TreeModel::AppendTreeData(TreeItem* pTreeData)
{
    if (nullptr == m_rootItem)
    {
        QList<QVariant> lV;
        lV.append("tree root");
        m_rootItem = new TreeItem(lV, nullptr);
    }

    m_rootItem->appendChild(pTreeData);
}

QVariant TreeModel::GetNodeText(const QModelIndex &index)
{
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    if (childItem)
        return childItem->data(0);
    return "";
}

void TreeModel::SetRole(int nIndex, QByteArray qByteAr)
{
    m_roleNames[nIndex] = qByteAr;
}

